<?php

namespace app\modules\admin\services;

use app\common\services\BaseService;
use app\components\pagination\Pagination;
use app\models\PrintMachine;
use app\modules\admin\forms\printMachine\PrintForm;

class PrintService extends BaseService{

    /**
     * 搜索
     * @param $sn
     * @param $page
     * @param $pagesize
     * @return array|\yii\db\ActiveRecord[]
     */
    public static function search($sn,&$page,$pagesize){
        $query = PrintMachine::find();

        if(!empty($sn)){
            $query->andWhere(['sn'=>$sn]);
        }

        $page = new Pagination(['totalCount' => $query->count(), 'pageSize' => $pagesize]);
        $query->offset($page->getOffset());
        $query->limit($page->getLimit());

        return $query->all();
    }

    /**
     * 创建
     * @param PrintForm $form
     * @return bool
     * @throws \Throwable
     */
    public static function create(PrintForm $form){
        if(!$form->validate()){
            self::setError($form->getFirstFieldError());
            return false;
        }

        $model = new PrintMachine();
        $model->setAttributes($form->getAttributes(),false);
        $model->setAttribute("add_time",date("Y-m-d H:i:s"));
        return $model->insert() !== false;
    }

    /**
     * 获取信息
     * @param $id
     * @return PrintMachine|null
     */
    public static function getInfoById($id){
        return PrintMachine::findOne($id);
    }

    /**
     * 更新
     * @param PrintForm $form
     * @return bool
     * @throws \Throwable
     * @throws \yii\db\StaleObjectException
     */
    public static function update(PrintForm $form){
        if(!$form->validate()){
            self::setError($form->getFirstFieldError());
            return false;
        }

        $model = PrintMachine::findOne($form->print_id);
        if(!$model){
            self::setError(\Yii::t("app","打印机不存在"));
            return false;
        }
        $model->setAttributes($form->getAttributes(),false);
        return $model->update() !== false;
    }

    public static function delete($id){
        PrintMachine::deleteAll(['print_id'=>$id]);
    }

    /**
     * 绑定
     * @param $id
     * @param $merchant_id
     * @return bool
     * @throws \Throwable
     * @throws \yii\db\StaleObjectException
     */
    public static function bind($id,$merchant_id){
        $model = PrintMachine::findOne($id);
        if(!$model){
            self::setError(\Yii::t("app","打印机不存在"));
            return false;
        }
        if($model->merchant_id != 0){
            self::setError(\Yii::t("app","打印机已被绑定,请先解绑!"));
            return false;
        }
        $model->setAttribute("merchant_id",$merchant_id);
        return $model->update() !== false;
    }

    /**
     * 解绑
     * @param $id
     * @return bool
     * @throws \Throwable
     * @throws \yii\db\StaleObjectException
     */
    public static function unbind($id){
        $model = PrintMachine::findOne($id);
        if(!$model){
            self::setError(\Yii::t("app","打印机不存在"));
            return false;
        }
        $model->setAttribute("merchant_id","");
        return $model->update() !== false;
    }
}